#::=# BRAINFUNCT interpreter in THUE
#::=# by Frederic van der Plancke; released to the Public Domain.
#::=#
#::=# you must provide a rule that transforms * into your Brainfunct
#::=# program & data in the following order:
#::=#    (*) your BrainFunct program, without any non-Brainfunct character
#::=#        (no space then ! BF characters are: + - < > , . [ ])
#::=#    (*) a MANDATORY semicolon (':')
#::=#    (*) (facultative) the input for your BF program, in binary, each number
#::=#        ending with _; a null item marks end-of-file. (Input numbers should
#::=#        be in normal form: no leading zero unless the number itself is null.
#::=#        0 is represented as '0', not ''.)
#::=# 
#::=# Some BF program errors are automatically corrected, though:
#::=#    (*) decrementing zero (see below)
#::=#    (*) missing ']', _if_ you're lucky enough !
#::=# 
#::=# (To read your program and/or input from stdin, check you Thue manual
#::=# about ':::'. E.g. *::=:::).
#::=#
#::=# Output: will go on stdout under the same binary format as input.
#::=#
#::=# BF Language remarks: the implemented dialect is "ideal brainfunct":
#::=#   (*) when ','(input) encounters end of file, it stores 0 at current
#::=#       position;
#::=#   (*) a cell can store any non-negative value, there is no wrap-around
#::=#       at 256;
#::=#   (*) negative values are forbidden; decrementing zero yields zero
#::=#       (and an error message).
#::=#
#::=# Current BF program computes and outputs the sum of 101 and 11.
#::=#
#::=# ****WARNING****
#::=# Any non-compliance with the above is likely to yield unwanted results,
#::=# ranging from halting the interpreter to infinitely exploding useless
#::=# computations (as if interpreting a BrainFunct program wasn't already
#::=# useless enough). 
#::=# In particular, avoid '#' characters in the input.
#::=#
#::=# ****WARRANTY****
#::=# In two words: no warranty.
#::=# This should not be used in nuclear plants or airplanes.


#::=# EXAMPLE OF PROGRAMS - remove the '#' before them to active them;
#::=#                   Thue will randomly chose among active programs.
#::=# 
#::=#------------------------- TEST PROGRAM #0: USER INPUT IS THE PROGRAM

#*::=:::

#::=#------------------------- TEST PROGRAM #1: a decrementation exercise

#*::=+++[-]:EOF

#::=#------------------------- TEST PROGRAM #2: addition of two input numbers

*::=,>,[-<+>]<.:101_11_

#::=#------------------------- TEST PROGRAM #3: idem with oddities

#*::=-,>,[-<+>>[-]<]<.:11_101_

#::=#------------------------- TEST PROGRAM #4: a decrementation exercise

#*::=+++++[.-]:

#::=#------------------------- TEST PROGRAM #5: missing ']', spotted

#*::=[-:

#::=#------------------------- TEST PROGRAM #6: missing ']', unspotted

#*::=+[-:

#::=#------------------------- you don't need to modify anything after this line

o0::=0o
i0::=0i
o1::=1o
i1::=1i
o]::=]o
i]::=]i
o[::=[o
i[::=[i
oz::=zo
oZ::=Zo
iz::=zi
iZ::=Zi
0z::=z0
1z::=z1
]z::=z]
[z::=z[
_z::=z_
0Z::=Z0
1Z::=Z1
]Z::=Z]
[Z::=Z[
_Z::=Z_
}}]::=}]}
&}]::=]&}
&]::=]^
}[::=[{
&[::=[&{
&0::=0&
&1::=1&
}0::=0}
{1::=1{
?Z[::=[&
?z[::=[^
?z]::=\]
?Z]::=]^
[{{::={[{
[{\::=\[
[\::=[^
]{::={]
]\::={\]
0\::=\0
1\::=\1
0{::={0
1{::={1
^[::=?[iii
^]::=?]iii
}|::=}]|WARN]WARN
&|::=&]|WARN]WARN
WARN]WARN::=~ERROR: missing ']' in brainfunct program; inserted at end
^000::=000^ooo
^001::=001^ooi
^010::=010^oio
^011::=011^oii
^100::=100^ioo
^101::=101^ioi
ooo|::=|ooo
ooi|::=|ooi
oio|::=iio|oio
oii|::=|oii
ioo|::=|ioo
ioi|::=|ioi
iii|::=|iii
iio|!::=|
|z::=z|
|Z::=Z|
o_::=_o
i_::=_i
0!::=!0
1!::=!1
_!::=!_
/!::=!/
oooX!::=Xooo
oooY::=$Y
0$::=!1
1$::=$0
X$::=X!1
ooiX!::=Xooi
ooiY::=@1Y
1@1::=@00
0@1::=@11
1@0::=@01
0@0::=@00
X@1::=X!WARNDEC0WARN
WARNDEC0WARN::=~WARNING: attempt at decrementing zero (result is still zero)
X@00::=X@0
X@01::=X!1
X@0Y::=X!0Y
oioX!::=LLYLR
0LL::=LL0
1LL::=LL1
_LL::=!X!
|LL::=|!0X!
LR0::=0LR
LR1::=1LR
LRY::=_
oiiX!::=_oii
oiiY::=X!%
%0::=0%
%1::=1%
%_0::=Y0
%_1::=Y1
%_/::=Y0_/
iooX!::=X(in)
(in)0::=(in)
(in)1::=(in)
(in)Y::=Yi
i/0::=Zi/
i/1::=zi/
i/_::=!/
XY!::=X!0Y
0Y!::=0!Y
1Y!::=1!Y
YZ::=0Y
Yz::=1Y
ioiX!::=X(out)
(out)0::=0(out)O0
(out)1::=1(out)O1
(out)Y::=OO!Y
O0::=~0
O1::=~1
OO::=~_
iiiX!0::=ZX!0
iiiX!1::=zX!1
+::=000
-::=001
<::=010
>::=011
,::=100
.::=101
:::=|X!0Y0_/
::=
^*
